import axios from 'axios'
import { Message } from 'element-ui'
import { baseUrl } from '../static/config/config'
import qs from 'qs'

axios.defaults.timeout = 60000
axios.defaults.baseURL = baseUrl

// http request 拦截器
axios.interceptors.request.use(
  config => {
    let contentType = 'application/x-www-form-urlencoded;charset=UTF-8;multipart/form-data'
    if (config.url.indexOf('aliOss/uploadImgFile') !== -1) {
      contentType = 'multipart/form-data'
    }else if(config.url.indexOf('proInquire/planDec/saveFile') !== -1){
      contentType = 'application/json'
    }else if(config.url.indexOf('/proInquire/planDec/save ')!== -1){
      contentType = 'application/json'
    }else if(config.url.indexOf('proInquire/earlyEdc/save')!== -1){
      contentType = 'application/json'
    }else if(config.url.indexOf('/proInquire/earlyEdc/selectReview')!== -1){
      contentType = 'application/json'
    }else if(config.url.indexOf('/proInquire/planDec/getFileInfo')!== -1){
      contentType = 'application/json'
    }else if(config.url.indexOf('/proInquire/planDec/load')!== -1){
      contentType = 'application/json'
    }else if(config.url.indexOf('/proInquire/earlyEdc/load')!== -1){
      contentType = 'application/json'
    }else if(config.url.indexOf('/proInquire/planDec/deleteProFile')!== -1){
      contentType = 'application/json'
    }
    else {
      config.data = qs.stringify(config.data)
    }
    
    config.headers = {
      'Content-Type': contentType
    }
    // 封装token
    let token = localStorage.getItem('jdGov')
    if(token) {
      config.headers.token = token
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// http response 拦截器
axios.interceptors.response.use(
  response => {
    // 未登录
    if (response.data.code === '502') {
      // loginInfo.dispatch('loginout')
      localStorage.removeItem('ms_username');
      location.href = '/login'
    } else if (response.data.code === '4000') {
      Message.error(response.message)
    } else if (response.data.code === '405') {
      Message.info(response.data.msg)
    } else {
      return response
    }
  }
)

/**
 * get请求
 * @param url
 * @param params
 * @returns {Promise<any>}
 */
export function get (url, params = {}) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: params
    }).then(response => {
      if (response) {
        resolve(response.data)
      }
    }).catch(error => {
      reject(error)
    })
  })
}

/**
 * post请求
 * @param url
 * @param data
 * @returns {Promise<any>}
 */
export function post (url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.post(url, data).then(response => {
      if (response) {
        resolve(response.data)
      }
    }, error => {
      reject(error)
    })
  })
}

/**
 * 规划类文件上传
 * @param url
 * @param data
 * @returns {Promise<any>}
 */
 export function fileUpload (fileObj,parma) {
  var form = new FormData()
  for(let file of fileObj){
    form.append('fileList',file)
  }


  for (let name in parma) {
    let value= parma[name]
    form.append(name,value)
}
  
  return new Promise((resolve, reject) => {
    axios.post('/proInquire/planDec/save ', form).then(response => {
      if (response) {
        resolve(response.data)
      }
    }, error => {
      reject(error)
    })
  })
}


/**
 * 非规划类文件上传
 * @param url
 * @param data
 * @returns {Promise<any>}
 */
 export function fileUploads (fileObj,parma) {
  var form = new FormData()
  for(let file of fileObj){
    form.append('fileList',file)
  }

  for (let name in parma) {
    let value= parma[name]
    form.append(name,value)
}
  
  return new Promise((resolve, reject) => {
    axios.post('proInquire/earlyEdc/save ', form).then(response => {
      if (response) {
        resolve(response.data)
      }
    }, error => {
      reject(error)
    })
  })
}


/**
 * 规划类文件上传
 * @param url
 * @param data
 * @returns {Promise<any>}
 */
 export function uploadDetailsFiles (fileObj = {}) {
  var form = new FormData()
  form.append('file', fileObj)
  return new Promise((resolve, reject) => {
    axios.post('/proInquire/planDec/load', form).then(response => {
      if (response) {
        resolve(response.data)
      }
    }, error => {
      reject(error)
    })
  })
}

/**
 * 非规划类文件上传
 * @param url
 * @param data
 * @returns {Promise<any>}
 */
 export function uploadsFiles (fileObj = {}) {
  var form = new FormData()
  form.append('file', fileObj)
  return new Promise((resolve, reject) => {
    axios.post('/proInquire/earlyEdc/load', form).then(response => {
      if (response) {
        resolve(response.data)
      }
    }, error => {
      reject(error)
    })
  })
}




/**
 * post请求 文件下载
 * @param url
 * @param data
 * @returns {Promise<any>}
 */
export function exportExcel (url, data = {}) {
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      url: url,
      data: data,
      responseType: 'blob'
    }).then(
      res => {
        let url = window.URL.createObjectURL(new Blob([res.data]))
        let fileName = Date.parse(new Date()) + '.xlsx'
        let link = document.createElement('a')
        link.href = url
        link.download = fileName
        link.style.display = 'none'
        document.body.appendChild(link)
        link.click()
        window.URL.revokeObjectURL(link.href)
        document.body.removeChild(link)
      },
      err => {
        reject(err)
      }
    )
  })
}

/**
 * post请求 获取文件流
 * @param url
 * @returns {Promise<any>}
 */
 export function getFileStream (url) {
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      url: url,
      responseType: 'blob'
    }).then(
      res => {
        resolve(res.data)
      },
      err => {
        reject(err)
      }
    )
  })
}
